package org.niugang.controller;

import com.alibaba.fastjson.JSON;
import org.niugang.config.CustomPrincipal;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;


@Controller
public class LoginController {


    @GetMapping("/")
    public String index() {
        return "forward:/index";
    }

    @GetMapping("/index")
    public String nowIndex() {
        return "index";
    }

    @GetMapping("/welcome")
    public String welcome() {
        return "welcome";
    }

    /**
     * @param principal 存在登录成功后的信息
     * @return Object
     */
    @RequestMapping({"/getUserInfo"})
    @ResponseBody
    public Object user(Principal principal) {
        //重写之后 principal.getName()存放是 CustomPrincipal toString形式的数据
        String jsonObject = principal.getName();
        if (jsonObject != null) {
            CustomPrincipal customPrincipal = JSON.parseObject(jsonObject, CustomPrincipal.class);
            return customPrincipal;
        }
        return new Object();
    }
}
